<?php
/*
 * @Description:
 * @Autor: wangjt
 * @Date: 2020-04-08 14:54:46
 * @LastEditors: wangjt
 * @LastEditTime: 2020-04-17 15:44:00
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;

class SessionController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest', [
            'only'=>['create']
        ]);
    }

    public function create()
    {
        return view('sessions.create');
    }

    public function store(Request $request)
    {
        $checked = $this->validate($request, [
            'email' => 'required|email|max:255',
            'password' => 'required'
        ]);

        if ( Auth::attempt($checked, $request->has('remember')) ) {
            session()->flash('success', '欢迎回来');
            // return redirect()->route('users.show', [Auth::user()]);
            $fallback = route('users.show', Auth::user());
            return redirect()->intended($fallback);
        }else {
            session()->flash('danger', '很抱歉，您的邮箱和密码不匹配');
            return redirect()->back()->withInput();
        }

        return;
    }

    public function destroy()
    {
        Auth::logout();
        session()->flash('success', '您已成功退出！');
        return redirect('login');
    }
}
